package com.wx.mall.operate.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.wx.mall.common.utils.UploadUtil;
import com.wx.mall.operate.dto.AdDto;
import com.wx.mall.operate.entity.Ad;
import com.wx.mall.operate.mapper.AdMapper;
import com.wx.mall.operate.service.IAdService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author j
 * @since 2020-05-11
 */
@Service
public class AdServiceImpl extends ServiceImpl<AdMapper, Ad> implements IAdService {

    @Value("${localhost.file.path}")
    private String filePath;

    @Resource
    private UploadUtil uploadUtil;


    @Override
    public List<Ad> getList(AdDto dto) {

        PageHelper.startPage(dto.getPageNum(),dto.getPageSize());

//        获取查询条件

        QueryWrapper<Ad> wrapper=new QueryWrapper<>();

        if(!dto.getName().equals("")){
            wrapper.like("name",dto.getName());
        }

        if(!dto.getPosition().equals("")){
            wrapper.like("position",dto.getPosition());
        }
//获取当前时间
        Calendar calendar=Calendar.getInstance();
        calendar.setTime(new Date());

        switch (dto.getEndTimeType()){

            case 1:

                calendar.add(Calendar.DAY_OF_MONTH,-1);
                Date date=calendar.getTime();
                wrapper.le("end_time",date);
                break;
            case 2:
                calendar.add(Calendar.DAY_OF_MONTH,-3);
                Date date2=calendar.getTime();
                wrapper.le("end_time",date2);
                break;

            case 3:
                calendar.add(Calendar.DAY_OF_MONTH,-7);
                Date date3=calendar.getTime();
                wrapper.le("end_time",date3);
                break;

        };

//        排序
switch (dto.getSort()){
    case 0:
        wrapper.orderByAsc("name");
        break;
    case 1:
        wrapper.orderByAsc("position");
        break;
}

List<Ad> list=this.list(wrapper);

        return list;
    }

    @Override
    public Map<String, String> uploadImg(MultipartFile file, String name) {

        return uploadUtil.uploadMD5(file,filePath,name);
    }
}
